<?php
/**
 * pdo_mssql驱动
 * 
 * @package if
 * @subpackage module.db.drivers.pdo
 * @since 1.0
 */
class IDbPdoMssql extends IAbstractDbPdo {
	protected $pdoType = "mssql";
	
	protected $startQuote = "[";
	protected $endQuote = "]";
	
	protected $support = array(
		"offset" => false
	);
	
	protected $metaTableSQL = "select name,case when type='U' then 'T' else 'V' end from sysobjects where (type='U' or type='V') and (name not in ('sysallocations','syscolumns','syscomments','sysdepends','sysfilegroups','sysfiles','sysfiles1','sysforeignkeys','sysfulltextcatalogs','sysindexes','sysindexkeys','sysmembers','sysobjects','syspermissions','sysprotects','sysreferences','systypes','sysusers','sysalternates','sysconstraints','syssegments','REFERENTIAL_CONSTRAINTS','CHECK_CONSTRAINTS','CONSTRAINT_TABLE_USAGE','CONSTRAINT_COLUMN_USAGE','VIEWS','VIEW_TABLE_USAGE','VIEW_COLUMN_USAGE','SCHEMATA','TABLES','TABLE_CONSTRAINTS','TABLE_PRIVILEGES','COLUMNS','COLUMN_DOMAIN_USAGE','COLUMN_PRIVILEGES','DOMAINS','DOMAIN_CONSTRAINTS','KEY_COLUMN_USAGE','dtproperties'))";
	
	function nConnect() {
		parent::nConnect("odbc:Driver={SQL Server};Server={$this->config['host']};Database={$this->config['dbname']};");
	}
	
	/**
	 * Enter description here...
	 * 
	 * 
	 * @param unknown_type $name
	 * @return array
	 */
	function describeTable($name) {
  	   	$rows = $this->fetchAll($this->nativeExec("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('{$name}', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('{$name}'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{$name}'"));
	   	$fields = array();
	   	foreach ($rows as $row) {
	   		$column = $row["Field"];
	   		$fields[$column] = array(
	   			"name" =>$column,
	   			"type" => $row["Type"],
	   			"max_length" => $row["Length"],
	   			"not_null" => $row["Null"],
	   			"default_value" => $row["Default"],
	   			"primary_key" => $row["Key"],
	   		);
	   		$fields[$column]["data_type"] = $this->dataType($fields[$column]);
	   	}
	   	return $fields;
	}	
	
	
	function nLimit($sql, $count, $offset = 0) {
		return preg_replace("/^SELECT\s/i", "SELECT TOP " . ($count + $offset) . " ", $sql);
	}
}


?>